package com.supermap.test;

import com.supermap.data.*;
import com.supermap.data.conversion.*;

public class GdbImport {
    public static void main(String[] args) {
        try {
            String gdbPath = "E:\\MapData\\202411\\数据库.gdb";
            String udbxPath = "E:\\MapData\\202411\\DataSource2.udbx";

            Workspace workspace = new Workspace();
            WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(
                    "E:\\MapData\\202410\\WorkSpaceYS.smwu");
            conInfo.setType(WorkspaceType.SMWU);
            workspace.open(conInfo);
            importGDB(gdbPath, null, workspace.getDatasources().get("DataSource2"), null);
        } catch (Exception e) {
            System.out.println("gdb2Udbx失败");
            e.printStackTrace();
        }



    }

    public static void importGDB(String sourceFile, String sourceLayerName, Datasource targetDatasource, String targetLayerName) {
        DataImport dataImport = new DataImport();
        ImportSettingFileGDBVector importSetting = new ImportSettingFileGDBVector();
        try {
            importSetting.setImportMode(ImportMode.NONE);
            importSetting.setSourceFileCharset(Charset.UTF8);
            System.out.println(importSetting.getSourceFileType());
            importSetting.setImportEmptyDataset(true);
            System.out.println("*******************************开始导入gdb数据" + sourceFile + "***************************");
            importSetting.setSourceFilePath(sourceFile);
            importSetting.setTargetDatasourceConnectionInfo(targetDatasource.getConnectionInfo());
            dataImport.getImportSettings().add(importSetting);

            //添加进度监听
            dataImport.addImportSteppedListener(importSteppedEvent -> System.out.println("totalPercent:" + importSteppedEvent.getTotalPercent()));

            ImportResult result = dataImport.run();
            System.out.println("*******************************gdb数据转为udbx成功" + sourceFile + "***************************");
            if (result.getSucceedSettings() != null && result.getSucceedSettings().length > 0) {
                System.out.println("success");
                result.getSucceedDatasetNames(result.getSucceedSettings()[0]);
            } else {
                System.out.println("fail");
            }
        } catch (Exception e) {
            throw new RuntimeException("导入GDB文件失败", e);
        } finally {
            dataImport.dispose();
            importSetting.dispose();
        }

    }
}
